﻿using Sidvall.UI.Dialogs;
using System.Threading.Tasks;

namespace Sidvall.UI.Wpf.Dialogs
{
    public partial class WebSecurityDialog
    {
        #region Public Members

        #region ShowAsync

        public Task ShowAsync(LogOnDialogContext logOnContext, LogOffDialogContext logOffContext, object owner)
        {
            if ((logOnContext == null) && (logOffContext == null))
                return Task.FromResult(true);
            this.LogOnContext = logOnContext;
            WindowManager.SetWindowOwner(this, owner);
            if (logOffContext != null)
            {
                this.WebSecurityControl.InitializeContext(logOffContext);
                this.Title = "Log Off";
                ShowDialog();
            }
            else
            {
                ShowLogOn();
            }
            return Task.FromResult(true);
        }

        #endregion

        #endregion
        #region Private Members

        private LogOnDialogContext LogOnContext { get; set; }

        #region ShowLogOn

        private void ShowLogOn()
        {
            if (this.LogOnContext == null)
                return;
            this.WebSecurityControl.InitializeContext(this.LogOnContext);
            this.Title = "Log On";
            this.LogOnContext = null;
            if (this.Visibility != System.Windows.Visibility.Visible)
                ShowDialog();
        }

        #endregion

        #endregion
        #region Events

        #region WebSecurityControl_CloseSelected

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private void WebSecurityControl_CloseSelected(object sender, System.EventArgs e)
        {
            if (this.LogOnContext != null)
            {
                ShowLogOn();
            }
            else
            {
                Close();
            }
        }

        #endregion

        #endregion
        #region Constructors

        public WebSecurityDialog()
        {
            InitializeComponent();

            Sidvall.UI.Wpf.ControlManager.SetLanguage(this);
        }

        #endregion
    }
}
